package org.karelun.gef.parts;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.karelun.gef.model.DiagramModel;
import org.karelun.gef.model.Node;
import org.karelun.gef.policies.DiagramLayoutEditPolicy;

public class DiagramPart extends AbstractGraphicalEditPart implements PropertyChangeListener {

    protected List<Node> getModelChildren() {
    	DiagramModel diagram=(DiagramModel)getModel();
    	List<Node> nodes = diagram.getNodes();
    	return nodes;
    }
    
//    @Override
//    protected void refreshChildren() {
//    	super.refreshChildren();
//    	updateChildrenPosition();
//    }
//    
//    private void updateChildrenPosition() {
//    	DiagramModel diagram=(DiagramModel)getModel();
//    	List<Node> nodes = diagram.getNodes();
//    	for(Node node:nodes){
//
//    	}
//	}
    
	public void activate() {
        super.activate();
        ((DiagramModel) getModel()).addPropertyChangeListener(this);
    }

    public void deactivate() {
    	((DiagramModel) getModel()).removePropertyChangeListener(this);
        super.deactivate();
    }

    public void propertyChange(PropertyChangeEvent evt) {
        String prop = evt.getPropertyName();
        if (DiagramModel.PROP_NODE.equals(prop)) 
            refreshChildren();
    }

    protected IFigure createFigure() {
        Figure figure = new FreeformLayer();
        figure.setLayoutManager(new FreeformLayout());
//        figure.setBackgroundColor(Display.getCurrent().getSystemColor(SWT.COLOR_CYAN));
        return figure;
    }

    protected void createEditPolicies() {
        installEditPolicy(EditPolicy.LAYOUT_ROLE, new DiagramLayoutEditPolicy());
    }
}